<?php

/**
* 内容页控制器
*/
class ContentController extends CommonController{

    /*内容页
     * */
    public function index(){
//  	p(cart::getGoods());
		
        $this->topNav();
		$gid=Q('get.gid',0,'intval');
		$goodsData=M()->join("__goods__ as g join __goods_detail__ as gd on g.gid=gd.goods_gid join brand as b on g.brand_bid=b.bid")->where("gid={$gid}")->find();
		//处理面包屑导航
		$bread=$this->model->getFather($this->allCate,$goodsData['category_cid']);
		$bread=array_reverse($bread);
		array_push($bread,$goodsData['gname']);
		$this->assign('bread',$bread);
		//把保存大中小图的路径由字符串拆分成数组
		$goodsData['simg']=explode('|',trim($goodsData['simg'],'|'));
		$goodsData['mimg']=explode('|',trim($goodsData['mimg'],'|'));
		$goodsData['bimg']=explode('|',trim($goodsData['bimg'],'|'));
		$this->assign('goods',$goodsData);
		//商品属性
		$attr=M()->join("__goods_attr__ as ga join type_attr as ta on ga.tp_aid=ta.tp_aid")->where("goods_gid={$gid} and tp_aclass=1")->all();
		$temp=array();
		foreach ($attr as $k => $v) {
			$temp[$v['tp_aname']][]=$v;
			
		}
		$temp=array_reverse($temp);
		$this->assign('attr',$temp);
        $this->display();
    }
	
	/*异步获得货品的库存
	 **/
	public function ajaxInventory(){
	    if(!IS_AJAX) return;
	    $gid=Q('get.gid',0,'intval');
	    $combine=Q('post.combine','');
	    $combine=explode(',', trim($combine,','));
	    $res=M('goods_list')->where("goods_gid={$gid}")->getField('glid,inventory,combine',true);
		$goodsInventory=array();
		foreach ($res as $k => $v) {
			$temp=array_diff($combine, explode(',',$v['combine']));
			if(empty($temp)){
				$goodsInventory['inventory']=(int)$v['inventory'];
				$goodsInventory['glid']=$v['glid'];
				break;
			}
		}
		$this->ajax($goodsInventory);
	}
	
	/*添加到购物车
	 * */
	public function ajaxCart(){
	    if(!IS_AJAX) return;
		cart::add($_POST);
		echo 1;die;
				
	}
	


}
    
    
    
?>